package com.fwx.common.core.domain.domain;

import com.fwx.common.core.domain.enums.ResultCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class R<T> {

    private int code;//定义一些固定code

    private String msg;

    private T data; //请求某个接口返回的数据

    public static <T> R<T> ok() {
        return assembleResult(null,ResultCode.SUCCESS);
    }

    public static <T> R<T> ok(T data) {
        return assembleResult(data,ResultCode.SUCCESS);
    }

    public static <T> R<T> fail() {
        return assembleResult(null,ResultCode.FAILED);
    }

    public static <T> R<T> fail(ResultCode resultCode) {
        return assembleResult(null,resultCode);
    }

    /*指定错误码*/
    private static <T> R<T> assembleResult(T data, ResultCode resultCode) {
        R<T> R = new R<>();
        R.setCode(resultCode.getCode());
        R.setData(data);
        R.setMsg(resultCode.getMsg());
        return R;
    }

}
